package com.chx.three.mama.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chx.three.mama.R;
import com.chx.three.mama.model.HomeBeauty;
import com.chx.three.mama.utils.BitmapHelper;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

import java.util.LinkedList;

/**
 * Created by aaa on 15-3-24.
 */
public class HomeBeautyAdapter extends BaseAdapter {
    
    private LinkedList<HomeBeauty> homeBeauties;
    private Context context;

    public HomeBeautyAdapter(LinkedList<HomeBeauty> homeBeauties, Context context) {
        this.homeBeauties = homeBeauties;
        this.context = context;
    }

    @Override
    public int getCount() {
        return homeBeauties.size();
    }

    @Override
    public Object getItem(int position) {
        return homeBeauties.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView!=null){
            ret=convertView;
        }else {
            ret= LayoutInflater.from(context).inflate(R.layout.item_social_listview,parent,false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder== null){
            holder=new ViewHolder();
            holder.img=(ImageView)ret.findViewById(R.id.item_home_beauty_img);
            holder.face=(ImageView)ret.findViewById(R.id.item_home_beauty_face_img);
            holder.titles=(TextView)ret.findViewById(R.id.item_home_beauty_title);
            holder.nickname=(TextView)ret.findViewById(R.id.item_home_beauty_nickname);
            holder.reNum=(TextView)ret.findViewById(R.id.item_home_beauty_re_num);
            holder.circle_img=(ImageView)ret.findViewById(R.id.item_circle_img);

            ret.setTag(holder);
        }

        HomeBeauty homeBeauty = homeBeauties.get(position);
        String ss = homeBeauty.getTitle();
        holder.titles.setText(ss);
        ss=homeBeauty.getNickname();
        holder.nickname.setText(ss);
        ss=homeBeauty.getRe_num();
        holder.reNum.setText(ss);

        BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
        ss=homeBeauty.getUrl();

        DisplayMetrics metric = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度（像素）
        float height_img = 266.0f / 500 * (width-2*context.getResources().getDimension(R.dimen.list_img_margin));
        ViewGroup.LayoutParams params = holder.img.getLayoutParams();
        params.height=(int)height_img;
        params.width=(int)(width-2*context.getResources().getDimension(R.dimen.list_img_margin));
        holder.img.setLayoutParams(params);
        bitmapUtils.display(holder.img,ss);
//        ViewGroup.LayoutParams param1 = holder.circle_img.getLayoutParams();
//        param1.height=(int)height_img;
//        param1.width=(int)(width-2*context.getResources().getDimension(R.dimen.list_img_margin));
//        holder.circle_img.setLayoutParams(param1);

        ss=homeBeauty.getAvatar();
        bitmapUtils.display(holder.face,ss,new BitmapLoadCallBack<ImageView>() {
            @Override
            public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                int width = bitmap.getWidth();
                Bitmap dst = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
                Paint paint = new Paint();
                paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                Canvas canvas = new Canvas(dst);
                canvas.drawCircle(width / 2, width / 2, width / 2, paint);
                imageView.setImageBitmap(dst);
            }

            @Override
            public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

            }
        });

        return ret;
    }

    private static class ViewHolder{
        public ImageView img;
        public ImageView face;
        public TextView titles;
        public TextView nickname;
        public TextView reNum;
        public ImageView circle_img;
    }
}
